package com.ruoyi.web.controller.second;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.GloryDataDTO;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.GloryType;
import com.ruoyi.system.service.IGloryTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 荣誉类型Controller
 *
 * @author 王清江
 * @date 2021-01-29
 */
@RestController
@RequestMapping("/system/type")
public class GloryTypeController extends BaseController {
    @Autowired
    private IGloryTypeService gloryTypeService;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询荣誉类型列表
     */
    @PreAuthorize("@ss.hasPermi('system:type:list')")
    @GetMapping("/list")
    public TableDataInfo list(GloryType gloryType) {
        startPage();
        List<GloryType> list = gloryTypeService.selectGloryTypeList(gloryType);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('student:item:list')")
    @GetMapping("/getListAllTypeAndData")
    public List<GloryDataDTO> getListAllTypeAndData() {
        List<GloryDataDTO> gloryData = gloryTypeService.getGloryData();
        return gloryData;
    }


    /**
     * 刷新用户看到的荣誉类型缓存
     */
    @PreAuthorize("@ss.hasPermi('system:type:list')")
    @GetMapping("/flushCache")
    public AjaxResult flushCache() {
        gloryTypeService.flushCache();
        return AjaxResult.success("刷新缓存成功！");
    }

    /**
     * 导出荣誉类型列表
     */
    @PreAuthorize("@ss.hasPermi('system:type:export')")
    @Log(title = "荣誉类型", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(GloryType gloryType) {
        List<GloryType> list = gloryTypeService.selectGloryTypeList(gloryType);
        ExcelUtil<GloryType> util = new ExcelUtil<GloryType>(GloryType.class);
        return util.exportExcel(list, "type");
    }

    /**
     * 获取荣誉类型详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:type:query')")
    @GetMapping(value = "/{gloryTypeId}")
    public AjaxResult getInfo(@PathVariable("gloryTypeId") Long gloryTypeId) {
        return AjaxResult.success(gloryTypeService.selectGloryTypeById(gloryTypeId));
    }

    /**
     * 新增荣誉类型
     */
    @PreAuthorize("@ss.hasPermi('system:type:add')")
    @Log(title = "荣誉类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GloryType gloryType) {
        return toAjax(gloryTypeService.insertGloryType(gloryType));
    }

    /**
     * 修改荣誉类型
     */
    @PreAuthorize("@ss.hasPermi('system:type:edit')")
    @Log(title = "荣誉类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GloryType gloryType) {
        return toAjax(gloryTypeService.updateGloryType(gloryType));
    }

    /**
     * 删除荣誉类型
     */
    @PreAuthorize("@ss.hasPermi('system:type:remove')")
    @Log(title = "荣誉类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{gloryTypeIds}")
    public AjaxResult remove(@PathVariable Long[] gloryTypeIds) {
        return toAjax(gloryTypeService.deleteGloryTypeByIds(gloryTypeIds));
    }


    /**
     * 状态修改,方恒亲自添加
     */
    @PreAuthorize("@ss.hasPermi('system:type:edit')")
    @Log(title = "荣誉类型", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody GloryType gloryType) {
        gloryType.setUpdateBy(SecurityUtils.getUsername());
        gloryType.setUpdateTime(new Date());
        return toAjax(gloryTypeService.updateGloryStatus(gloryType));
    }

}
